package com.ebelter.temperaturegun.ui.activity;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.TemUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class SettingActivity extends TBaseActivity {


    public static final String TAG = "SettingActivity";
    private ImageView top_iv;
    private TextView top_tv1;

    private TextView st_gwjjzh_tv;
    private TextView st_unit_switch_tv;


    @Override
    protected int setContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        st_gwjjzh_tv=findViewById(R.id.st_gwjjzh_tv);
        st_unit_switch_tv=findViewById(R.id.st_unit_switch_tv);
        ViewUtils.displayView(top_tv1);
        top_tv1.setText(R.string.setting);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        updateWarringUnit();
        updateUnitMode();
    }

    //更新警告温度
    private void updateWarringUnit() {
        float warring = TemUtils.getUerWarringUnit();
        ViewUtils.setTextViewStr(st_gwjjzh_tv, "≥"+NumUtils.numBaoLiuWei1(warring)+"°C");
    }

    //更新温度的模式
    private void updateUnitMode() {
        int model = TemUtils.getUerUnit();
        ViewUtils.setTextViewStr(st_unit_switch_tv,model==0?"°C":"°F");
    }


    public void modifyWarringtem(View view) {
    }
    public void switchUnit(View view) {

       // startActivity(UnitSwitchActivity.class,null);
        Intent intent = new Intent(this,UnitSwitchActivity.class);
        startActivityForResult(intent,1);
    }
    public void userInfo(View view) {
        startActivity(UserInfoActivity.class,null);
    }
    public void about(View view) {
        startActivity(AboutActivity.class,null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0){
            updateUnitMode();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, "SettingActivity--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd == 1){
            updateUnitMode();
        }


    }
}
